package com.mabs.ground.client.common;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.place.shared.PlaceController;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.mabs.ground.client.main.ui.Main;
import com.mabs.ground.client.module.connexion.view.ConnexionView;
import com.mabs.ground.client.module.connexion.view.ConnexionViewImpl;
import com.mabs.ground.client.module.mod1.view.HelloView;
import com.mabs.ground.client.module.mod1.view.HelloViewImpl;
import com.mabs.ground.client.module.mod2.view.GoodbyeView;
import com.mabs.ground.client.module.mod2.view.GoodbyeViewImpl;
import com.mabs.ground.shared.model.ModelBean;

/**
 * @author <a href="Email: mohamedarbi.benslimane@allence-tunisie.com">Ben
 *         Slimane Arbi</a>
 * @version 1.0
 * @since 1.0 - 25 juil. 2014 - 19:09:13
 */
public class ClientFactoryImpl implements ClientFactory {
	private static final EventBus eventBus = new SimpleEventBus();
	private static final PlaceController placeController = new PlaceController(
			eventBus);
	private static final HelloView helloView = new HelloViewImpl();
	private static final GoodbyeView goodbyeView = new GoodbyeViewImpl();
	private static final ConnexionView connexionView = new ConnexionViewImpl();

	private final static Main main = new Main(placeController);

	private final static MyBeanFactory myBeanFactory = GWT
			.create(MyBeanFactory.class);;

	public EventBus getEventBus() {
		return eventBus;
	}

	public HelloView getHelloView() {
		return helloView;
	}

	public PlaceController getPlaceController() {
		return placeController;
	}

	public GoodbyeView getGoodbyeView() {
		return goodbyeView;
	}

	public Main getMain() {
		return main;
	}

	public ConnexionView getConnexionView() {
		return connexionView;
	}

	public MyBeanFactory getMyBeanFactory() {
		return myBeanFactory;
	}

	public <M extends ModelBean> String serializeToJson(M m) {
		AutoBean<M> bean = AutoBeanUtils.getAutoBean(m);
		return AutoBeanCodex.encode(bean).getPayload();
	}

	public <M extends ModelBean> M deserializeFromJson(String json,
			Class<M> clazz) {
		AutoBean<M> bean = AutoBeanCodex.decode(myBeanFactory, clazz, json);
		return bean.as();
	}

	public void getJsonRequest(String url, String requestData,
			RequestCallback requestCallback) throws RequestException {
		// Send request to server and catch any errors.
		// RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		// builder.sendRequest(requestData, requestCallback);

		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
		builder.setHeader("Content-Type", "application/json");
		builder.sendRequest(requestData, requestCallback);

	}
}
